1 00:00:00,490 --> 00:00:01,510 Welcome back. 2 00:00:01,510 --> 00:00:06,370 In this lecture we're going to take a look at a neat little function in the string library called string 3 00:00:06,370 --> 00:00:07,630 dot format. 4 00:00:07,660 --> 00:00:12,940 The format function allows us to format strings into different formats that we desire. 5 00:00:12,970 --> 00:00:17,800 As an example, let's say we wanted to be able to get a string that is formatted in a month slash day 6 00:00:17,800 --> 00:00:22,660 slash year format, and we can just simply take numbers like five for the month, three for the day 7 00:00:22,660 --> 00:00:30,490 and 2015 for the year, and convert it into a string that says something like 05/03/ 2015. 8 00:00:30,490 --> 00:00:33,940 We can do exactly that using the format function. 9 00:00:34,180 --> 00:00:38,260 So the format function takes one argument and that's a string. 10 00:00:38,260 --> 00:00:42,490 And inside of that string you have to place some things in there called directives. 11 00:00:42,490 --> 00:00:46,240 And these directives tell the function how to format the string. 12 00:00:46,360 --> 00:00:51,790 These directives act as placeholders and will be replaced by whatever else you provide to the format 13 00:00:51,790 --> 00:00:52,600 function. 14 00:00:52,630 --> 00:00:54,760 So what is a directive? 15 00:00:54,790 --> 00:01:01,390 Well, a directive is defined by putting a percentage symbol in a string and then following up with 16 00:01:01,390 --> 00:01:07,480 any flags a width parameter, a precision parameter and a specifier. 17 00:01:07,480 --> 00:01:11,110 This whole collective statement itself is called the directive. 18 00:01:11,140 --> 00:01:16,090 Now when you place these directives in a string, you're telling the formatting function to expect another 19 00:01:16,090 --> 00:01:18,850 argument that is going to replace the directive. 20 00:01:18,880 --> 00:01:24,250 Now, you can use these directives to insert other strings into a string and certain numbers, decimals, 21 00:01:24,250 --> 00:01:25,570 and many other values. 22 00:01:25,570 --> 00:01:29,050 So let's first take a look at the width parameter. 23 00:01:29,080 --> 00:01:35,080 Now this parameter is optional and it defines how many spaces in a string that a value that is going 24 00:01:35,080 --> 00:01:37,630 to replace the directive is going to take up. 25 00:01:37,630 --> 00:01:45,310 So for example, if I did something like percentage and three s, I'm saying that the string that is 26 00:01:45,310 --> 00:01:49,870 going to take up this directive is going to be three characters in length, and it should be placed 27 00:01:49,870 --> 00:01:50,770 right here. 28 00:01:50,770 --> 00:01:54,460 If the string is greater than three characters, then it will just put the whole string. 29 00:01:54,460 --> 00:02:00,100 However, if the string is less than three characters, the remaining unused space will be replaced 30 00:02:00,100 --> 00:02:01,360 with an empty space. 31 00:02:01,360 --> 00:02:07,360 So for example, if I pass a string to it that just says hi, then what gets replaced here at this directive 32 00:02:07,360 --> 00:02:13,090 will be a string that has a space and then hi, which totals three characters. 33 00:02:13,480 --> 00:02:19,810 Now the precision parameter tells the function how many decimal points we want to be placed in a string 34 00:02:19,810 --> 00:02:20,890 for a float. 35 00:02:20,890 --> 00:02:28,120 For example, if I wanted to put a floating point number in a string, I would put percentage F that 36 00:02:28,120 --> 00:02:32,290 would stand for a floating point number that would be replaced at this directive. 37 00:02:32,290 --> 00:02:38,290 However, if I only wanted to include numbers in this float up to, let's say the hundredths place, 38 00:02:38,290 --> 00:02:40,510 then I would put point two. 39 00:02:40,540 --> 00:02:46,390 So percentage point two F, which is telling the function to include only numbers up to the hundredth 40 00:02:46,390 --> 00:02:50,590 place, or the second decimal place after the decimal point. 41 00:02:50,950 --> 00:02:51,970 So enough explaining. 42 00:02:51,970 --> 00:02:55,480 Let's actually go ahead and take a look at some examples. 43 00:02:55,480 --> 00:02:57,850 So let's say I have two strings. 44 00:02:57,850 --> 00:03:02,590 I'm going to call one some string one set it to something like hello. 45 00:03:02,590 --> 00:03:08,830 And let's say I had another string called some string two and set it equal to a string like hi. 46 00:03:09,710 --> 00:03:14,270 Then what I could do is I could create a formatted string, I'll call it formatted, and it's going 47 00:03:14,270 --> 00:03:17,390 to be equal to the string dot format function. 48 00:03:17,390 --> 00:03:25,310 And we're going to pass a string in here that says this is percentage s and percentage s. 49 00:03:25,310 --> 00:03:29,600 So I've put two directives inside of the string one here and one here. 50 00:03:29,600 --> 00:03:34,340 And that means I'm going to have to pass two values that's going to replace these directives. 51 00:03:34,340 --> 00:03:38,330 In this case they have to be strings because I denoted them as strings. 52 00:03:38,330 --> 00:03:42,170 So in the first argument I could put some string one. 53 00:03:42,170 --> 00:03:45,290 And then in the second argument I could put some string two. 54 00:03:45,290 --> 00:03:50,330 So some string one, which is hello is going to replace the first directive, and some string two is 55 00:03:50,330 --> 00:03:52,310 going to replace the second directive. 56 00:03:52,310 --> 00:03:56,210 And that means we can go ahead and print this formatted string in the console. 57 00:03:56,210 --> 00:03:59,780 And we should get a string that says this is hello and hi. 58 00:03:59,780 --> 00:04:01,250 So if we run the game. 59 00:04:02,510 --> 00:04:02,960 There we go. 60 00:04:02,960 --> 00:04:03,860 We get our string. 61 00:04:03,860 --> 00:04:05,960 This is hello and hi. 62 00:04:06,080 --> 00:04:10,100 And just like that, we formatted the string by inserting other strings into it. 63 00:04:10,640 --> 00:04:12,590 Now we can also do this with numbers. 64 00:04:12,590 --> 00:04:13,850 I'm going to create a variable. 65 00:04:13,850 --> 00:04:18,650 I'll call it num formatted equal to the string dot format function. 66 00:04:18,650 --> 00:04:24,110 And to define that we want to insert a number into a string, we have to use one of these specifiers 67 00:04:24,110 --> 00:04:26,570 for numbers, which is percentage d. 68 00:04:26,690 --> 00:04:29,090 And this is for integers or numbers. 69 00:04:29,360 --> 00:04:33,920 So what we could do is I could create a string that says x is equal to percentage d. 70 00:04:33,950 --> 00:04:36,020 This is my first directive. 71 00:04:36,020 --> 00:04:39,950 And then I could do y is equal to percentage d. 72 00:04:40,220 --> 00:04:42,680 And then I have to pass those numbers. 73 00:04:42,680 --> 00:04:45,320 So for the first number I could do like 12. 74 00:04:45,320 --> 00:04:47,660 And for the second number I could do negative five. 75 00:04:47,660 --> 00:04:53,210 So this should give us a string that says x is equal to 12 and y is equal to negative five. 76 00:04:53,210 --> 00:04:56,960 So we can go ahead and print num formatted in the console. 77 00:04:57,810 --> 00:05:00,210 And we should get exactly that. 78 00:05:00,960 --> 00:05:02,010 And there we go. 79 00:05:02,040 --> 00:05:03,210 X is equal to 12. 80 00:05:03,210 --> 00:05:05,370 Y is equal to negative five. 81 00:05:06,230 --> 00:05:11,360 Now let's go ahead and take a look at the width parameter for our directive. 82 00:05:12,380 --> 00:05:17,360 So I'm going to print out the return from the String.format function. 83 00:05:17,360 --> 00:05:21,290 And I'm going to put a string in here that's just um, let's do three dots. 84 00:05:21,290 --> 00:05:22,850 And then we can define that. 85 00:05:22,850 --> 00:05:24,800 We want to put a number here. 86 00:05:24,800 --> 00:05:29,060 So we'll do percentage D and then we'll put another three dots. 87 00:05:29,240 --> 00:05:31,130 And then we could pass a number like ten. 88 00:05:31,130 --> 00:05:34,760 So this should print dot dot dot ten dot dot dot. 89 00:05:35,060 --> 00:05:40,010 Now let's say I wanted to put a bunch of empty space in front of the number ten. 90 00:05:40,010 --> 00:05:42,080 Well then I could pass a width here. 91 00:05:42,080 --> 00:05:48,350 I could put in a width of ten, which means whatever number is going to be replaced here, if it isn't 92 00:05:48,350 --> 00:05:51,530 equal to or greater than ten characters in width. 93 00:05:51,530 --> 00:05:57,680 In this case, our ten is only two characters, then it's going to fill up the rest of the unused space 94 00:05:57,680 --> 00:05:59,000 with empty spaces. 95 00:05:59,000 --> 00:06:04,670 So when this prints out, it should print dot dot dot a bunch of spaces and then ten dot dot dot. 96 00:06:04,670 --> 00:06:07,190 So let's go ahead and run and see what we get. 97 00:06:08,030 --> 00:06:08,840 And there we go. 98 00:06:08,840 --> 00:06:15,500 We get dot, dot, dot and then we get let's see 123456, seven, eight. 99 00:06:15,500 --> 00:06:19,040 That's eight empty spaces plus one and zero. 100 00:06:19,040 --> 00:06:21,020 So that's a total of ten. 101 00:06:21,020 --> 00:06:26,390 So again we told the function to expect a total of ten characters to be taken up. 102 00:06:26,390 --> 00:06:31,610 And since the number we passed only took up two, it filled up the rest with empty spaces. 103 00:06:32,210 --> 00:06:34,460 Now let's go ahead and take a look at something else a little bit cool. 104 00:06:34,460 --> 00:06:37,010 And we're going to take a look at flags. 105 00:06:37,010 --> 00:06:41,930 Now I haven't explained flags yet, but what flags are is that they're an extra bonus we can add to 106 00:06:41,930 --> 00:06:46,250 our directive to kind of further fine tune how we want our string to be formatted. 107 00:06:46,250 --> 00:06:52,310 So, for example, let's say I didn't want all of these spaces here to be in front of the number. 108 00:06:52,310 --> 00:06:56,630 Let's say I wanted the number to be first and then the empty spaces could come along well. 109 00:06:56,630 --> 00:07:02,690 I could put a flag in there, which is a minus symbol, and this will force the value, whatever is 110 00:07:02,690 --> 00:07:06,560 going to be replaced by this directive, whatever value it is, in this case it's ten. 111 00:07:06,560 --> 00:07:10,220 It's going to force it to be on the left side instead of the right side. 112 00:07:10,340 --> 00:07:13,310 So if we go and run our game, we should get there. 113 00:07:13,310 --> 00:07:14,960 We go dot dot dot ten. 114 00:07:14,960 --> 00:07:18,230 And then we get our eight empty spaces and then dot dot dot. 115 00:07:18,230 --> 00:07:21,470 So we just move the value from the end to the beginning. 116 00:07:22,320 --> 00:07:23,730 So that's pretty neat. 117 00:07:23,760 --> 00:07:28,020 Now let's go ahead and take a look at an example of using the precision parameter. 118 00:07:28,050 --> 00:07:33,030 So let's say I wanted to print out pi right. 119 00:07:33,030 --> 00:07:37,320 Math dot pi which is 3.14159 and so on. 120 00:07:37,320 --> 00:07:40,470 But let's say I only want to get the first four digits of pi. 121 00:07:40,500 --> 00:07:47,070 Well, what I could do is I'll create a variable, I'll just call it pi, and it'll be equal to string.format. 122 00:07:47,430 --> 00:07:52,080 And we'll put a string in here that says pi is equal to. 123 00:07:52,080 --> 00:07:53,760 And I'll put our directive in here. 124 00:07:53,760 --> 00:07:55,440 So we could do percentage. 125 00:07:55,560 --> 00:07:57,630 And again we want to define the precision here. 126 00:07:57,630 --> 00:08:01,680 So we want to go up to let's say the fourth decimal spot. 127 00:08:01,680 --> 00:08:03,450 So we could do 0.4. 128 00:08:03,450 --> 00:08:06,630 So it'll give us four digits after the decimal point. 129 00:08:06,630 --> 00:08:11,250 And then we'll make sure to put F here to denote this as a floating point number that we're going to 130 00:08:11,250 --> 00:08:13,590 be replacing right here at this directive. 131 00:08:13,590 --> 00:08:16,440 And then we can pass math dot pi. 132 00:08:16,620 --> 00:08:21,420 And this should print in the console three point and then four digits of pi. 133 00:08:21,420 --> 00:08:23,760 So let's go ahead and print out pi here. 134 00:08:26,240 --> 00:08:26,690 And there we go. 135 00:08:26,690 --> 00:08:29,030 We get 3.16. 136 00:08:29,030 --> 00:08:30,740 So it rounded up pi for us. 137 00:08:30,740 --> 00:08:34,190 But as you can see we only got the first four digits. 138 00:08:34,190 --> 00:08:38,600 If I wanted to get more than that, then I could define something like maybe eight and we'll get eight 139 00:08:38,600 --> 00:08:40,550 digits after the decimal point. 140 00:08:40,550 --> 00:08:41,270 There we go. 141 00:08:41,270 --> 00:08:43,550 Eight digits after the decimal point. 142 00:08:44,550 --> 00:08:48,000 So as you can see, the precision parameter isn't too complex. 143 00:08:48,000 --> 00:08:51,150 It just lets us decide how many numbers after the decimal point we want. 144 00:08:51,180 --> 00:08:53,190 For this particular floating point number. 145 00:08:53,960 --> 00:08:57,830 Now, that example I gave originally about formatting a date. 146 00:08:57,830 --> 00:08:59,690 Let's go ahead and do that right now. 147 00:08:59,690 --> 00:09:02,540 So I'm going to create a variable I'm going to call it date. 148 00:09:02,540 --> 00:09:03,920 Let me scroll this down here. 149 00:09:03,920 --> 00:09:06,620 And it's going to be equal to string dot format. 150 00:09:07,320 --> 00:09:09,810 And then we can format our date here. 151 00:09:09,840 --> 00:09:13,320 Now there's another cool flag we can use, which is a zero. 152 00:09:13,320 --> 00:09:14,460 And I've defined it up here. 153 00:09:14,460 --> 00:09:17,760 It says pads the value on the left side with zeros. 154 00:09:17,760 --> 00:09:23,610 So basically remember that when we defined the width of our string it just filled it up with a bunch 155 00:09:23,610 --> 00:09:24,870 of empty spaces. 156 00:09:24,870 --> 00:09:31,650 Well if we put a zero there as the flag, all of the empty spaces will be instead replaced with zeros. 157 00:09:31,650 --> 00:09:33,270 Yep, that's all it does. 158 00:09:33,270 --> 00:09:39,810 So that means what we could do is we could put a directive here like percentage and pass zero, meaning 159 00:09:39,810 --> 00:09:42,660 that any empty space will be filled with zeros. 160 00:09:42,660 --> 00:09:44,520 And then we can define the width. 161 00:09:44,520 --> 00:09:48,240 So for the days we only know the days go up to two digits. 162 00:09:48,240 --> 00:09:49,860 That means we can put two here. 163 00:09:50,130 --> 00:09:53,220 And then we'll specify it as a number with D. 164 00:09:53,340 --> 00:09:57,480 And then we could put a slash here and do the same thing for the days. 165 00:09:57,480 --> 00:09:59,250 So 02D. 166 00:09:59,280 --> 00:10:03,150 Well actually depending on how you like your calendar set up this could be months or days. 167 00:10:03,150 --> 00:10:05,010 And this could be months or days. 168 00:10:05,640 --> 00:10:07,830 And then for the last one it's going to be the year. 169 00:10:07,830 --> 00:10:09,210 And we want four digits. 170 00:10:09,210 --> 00:10:17,640 So we can just do percentage 04D which isn't really necessary because if you pass something like 19 171 00:10:17,640 --> 00:10:21,990 then it's going to say zero zero 19, even though you might want like 2019. 172 00:10:21,990 --> 00:10:25,020 But that's not an issue for us right now. 173 00:10:25,020 --> 00:10:30,540 But now what we could do is we could pass three values that are going to replace each of these directives. 174 00:10:30,540 --> 00:10:35,190 So let's say for the month, I want it to be the third month for the day. 175 00:10:35,190 --> 00:10:36,300 It could be the first. 176 00:10:36,300 --> 00:10:39,060 And then for the year I could do something like 2013. 177 00:10:39,640 --> 00:10:43,570 And now, because we've defined that we want to fill up the empty spaces with zero. 178 00:10:43,570 --> 00:10:48,130 When we pass three here, it's going to see, hey, this is only taking up one space. 179 00:10:48,130 --> 00:10:53,260 Even though we defined it to take up two spaces, that means the empty space is going to be filled with 180 00:10:53,260 --> 00:10:56,830 a zero, and that means it's going to be formatted as zero three. 181 00:10:56,860 --> 00:10:58,000 Same thing for the day. 182 00:10:58,000 --> 00:11:00,100 It will be formatted as zero one. 183 00:11:00,100 --> 00:11:03,490 And since 2013 takes up four spaces. 184 00:11:03,490 --> 00:11:05,800 Then it isn't going to put in any zeros for us. 185 00:11:05,800 --> 00:11:07,780 And it should just say 2013. 186 00:11:07,780 --> 00:11:14,500 So let's go ahead and print this date out into the console and we should get 0301 2013. 187 00:11:16,450 --> 00:11:17,170 And there we go. 188 00:11:17,170 --> 00:11:23,140 Just like that, we get 0301 2013 so it filled up that empty space with a zero. 189 00:11:23,140 --> 00:11:24,490 Did the same here. 190 00:11:24,490 --> 00:11:29,740 And then since this number filled up all of those four spaces that we defined, it didn't put any extra 191 00:11:29,740 --> 00:11:30,760 zeros in there. 192 00:11:31,030 --> 00:11:35,140 So that is the neat little power of the string dot format function. 193 00:11:35,140 --> 00:11:41,380 If you want to learn more about the different flags and the different specifiers that exist for the 194 00:11:41,380 --> 00:11:46,360 string format function, I'll leave an article attached to the lecture that goes much more into depth 195 00:11:46,360 --> 00:11:52,240 about all of these parameters and specifiers for our directives, but for now, this should give you 196 00:11:52,240 --> 00:11:56,740 a very good idea of how you can use the String.format function in your scripts. 197 00:11:56,950 --> 00:11:58,990 All right, that's all for me for this lecture. 198 00:11:58,990 --> 00:12:03,100 I hope you find this information useful, and maybe you'll start to use the String.format function in 199 00:12:03,100 --> 00:12:04,420 your own code soon. 200 00:12:04,420 --> 00:12:05,950 See you in the next one.